package com.yonyou.jrmob.application.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hss01248.dialog.StyledDialog;
import com.yonyou.jrmob.application.R;
import com.yonyou.jrmob.application.common.adapter.AppGridAdapter;
import com.yonyou.uap.um.service.ServiceCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.titlebar.BGATitleBar;
import jrmob.yonyou.com.commonlib.app_config_tool.constant.Constant;
import jrmob.yonyou.com.commonlib.app_config_tool.gkh.MAServices_gkh;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.LogUtils;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.StatusBarUtil;
import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;
import jrmob.yonyou.com.commonlib.vo.AppVO_ap;

public class AllAppActivity extends BaseActivityMob_lib {
    BGATitleBar titleBar;
    GridView myApp,recommendApp;
    List<AppVO_ap> mMyAppVO_aps = new ArrayList<>();
    List<AppVO_ap> mALLAppVO_aps = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_app);
        initView();
        initListener();
//        initData();
        callService();
        StatusBarUtil.StatusBarLightMode(this);
        StatusBarUtil.setStatusBarColor(this,R.color.white);
    }

    @Override
    public void initView() {
        titleBar = (BGATitleBar) findViewById(R.id.title_bar_all_app);
        myApp = (GridView) findViewById(R.id.all_app_my_app);
        recommendApp = (GridView) findViewById(R.id.all_app_recommend_app);
    }

    @Override
    public void initListener() {
        titleBar.setDelegate(new BGATitleBar.Delegate() {
            @Override
            public void onClickLeftCtv() {
                finish();
            }

            @Override
            public void onClickTitleCtv() {

            }

            @Override
            public void onClickRightCtv() {

            }

            @Override
            public void onClickRightSecondaryCtv() {

            }
        });
        myApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", mMyAppVO_aps.get(position));
                Intent intent = new Intent(AllAppActivity.this,AppDetailActivity.class);
                intent.putExtras(bundle);
//                startActivity(intent);
            }
        });
        recommendApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", mALLAppVO_aps.get(position));
                Intent intent = new Intent(AllAppActivity.this,AppDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {

        myApp.setAdapter(new AppGridAdapter(this, mMyAppVO_aps));
        int rowNum1;

        if(mMyAppVO_aps.size() % 4 != 0){
            rowNum1 = mMyAppVO_aps.size() / 4 + 1;
        } else {
            rowNum1 = mMyAppVO_aps.size() / 4;
        }
        int height1 = (int)(rowNum1 * (getResources().getDimension(R.dimen.app_grid_view_item_height) + getResources().getDimension(R.dimen.app_grid_view_item_vertical_spacing))
                - getResources().getDimension(R.dimen.app_grid_view_item_vertical_spacing));
        LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) myApp.getLayoutParams();
        lp1.height = height1;
        myApp.setLayoutParams(lp1);

        recommendApp.setAdapter(new AppGridAdapter(this, mALLAppVO_aps));
        int rowNum;
        if(mALLAppVO_aps.size() % 4 != 0){
            rowNum = mALLAppVO_aps.size() / 4 + 1;
        } else {
            rowNum = mALLAppVO_aps.size() / 4;
        }
        int height = (int)(rowNum * (getResources().getDimension(R.dimen.app_grid_view_item_height) + getResources().getDimension(R.dimen.app_grid_view_item_vertical_spacing))
                - getResources().getDimension(R.dimen.app_grid_view_item_vertical_spacing)
                + CommonUtil.dip2px(this,50));
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) recommendApp.getLayoutParams();
        lp.height = height;
        recommendApp.setLayoutParams(lp);
    }
    private void callService(){
        try {
            JSONObject json = new JSONObject();

            json.put("transtype", Constant.TRANSTYPE_APP_LIST);
            json.put("user_code", getUserInfoLRY().getUser_code());
            json.put("tenant_id", getUserInfoLRY().getTenant_id());
            json.put("uuidToken", Constant.TOKEN);
            if (!CommonUtil.isNetworkConnected(this)) {
                CommonUtil.showToast(this, "当前网络不可用,请检查!");
                return;
            }
            StyledDialog.buildMdLoading().show();
            MAServices_gkh.callMACommonService(this, new ServiceCallback() {
                @Override
                public void execute(JSONObject jsonObject) {
                    LogUtils.e("all app：" + jsonObject.toString());
                    try {
                        JSONObject obj = (JSONObject) jsonObject.opt("result");
                        // 有错误的时候
                        if (null != obj && obj.optString("retflag").equals("1")) {
                            String msg = obj.optString("message");
                            if (!msg.isEmpty() || !msg.equals("0")) {
                                showMessage(msg);
                            }
                            return;
                        }
                        StyledDialog.dismissLoading();
                        Gson gson = new Gson();
                        JSONArray my_app_arrJsonArray = obj.optJSONArray("my_app_arr");
                        if (null != my_app_arrJsonArray && my_app_arrJsonArray.length() != 0){
                            mMyAppVO_aps = gson.fromJson(my_app_arrJsonArray.toString(),new TypeToken<List<AppVO_ap>>(){}.getType());
                        }
                        JSONArray all_app_arrJsonArray = obj.optJSONArray("all_app_arr");
                        if (null != all_app_arrJsonArray && all_app_arrJsonArray.length() != 0){
                            mALLAppVO_aps = gson.fromJson(all_app_arrJsonArray.toString(),new TypeToken<List<AppVO_ap>>(){}.getType());
                        }
                        AppVO_ap scheduleAppVO_ap = new AppVO_ap();
                        scheduleAppVO_ap.setApp_name("工作日历");
                        mMyAppVO_aps.add(scheduleAppVO_ap);
                        if(mALLAppVO_aps != null && mALLAppVO_aps.size() != 0){
                            initData();
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void error(String s, String s1) {
                    StyledDialog.dismissLoading();
                    LogUtils.e(s + " === " + s1);
                }
            }, json);
        } catch (Exception e) {
            StyledDialog.dismissLoading();
            e.printStackTrace();
        }
    }
}
